package com.wfh.util;

import com.wfh.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MyEditText extends LinearLayout {
	private Button plus;
	private Button sub;
	private EditText edit;
	private OnChangeListener listener;
	public MyEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		LayoutInflater.from(getContext()).inflate(R.layout.myedittext, this);
		initView();

	}

	public void initView() {
		plus = (Button) findViewById(R.id.button2);
		sub = (Button) findViewById(R.id.button1);
		edit = (EditText) findViewById(R.id.editText1);

		plus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int num = Integer.valueOf(edit.getText().toString());
				num++;
				edit.setText(Integer.toString(num));
				listener.onChange();
			}
			
		});

		sub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int num = Integer.valueOf(edit.getText().toString());
				if(num==0){
					return;
				}
				num--;
				edit.setText(Integer.toString(num));
				listener.onChange();
			}
		});
	}
	
	
	public String getNumber(){
		return edit.getText().toString();
	}
	
	public void setNumber(String number){
		edit.setText(number);
	}
	
	public void setOnChangeListener(OnChangeListener listener){
		this.listener=listener;
	}

	public interface OnChangeListener{
		void onChange();
	}
	
}
